package cn.umakr.web;

import cn.umakr.entity.Dept;
import cn.umakr.entity.User;
import cn.umakr.entity.UserInfo;
import cn.umakr.service.*;
import cn.umakr.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by wangbo on 2017/7/20.
 */
@WebServlet(name = "ListServlet", urlPatterns = "/ListServlet")
public class ListServlet extends HttpServlet {

    private UserService userService = new UserService();
    private DeptService deptService = new DeptService();
    private UserInfoService userInfoService = new UserInfoService();
    private DeptPageService deptPageService = new DeptPageService();
    private UserPageService userPageService = new UserPageService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String type = request.getParameter("type");
        String current = request.getParameter("currentpage");
        if (type.equals("user")) {
            //查询用户列表数据
            List<UserInfo> userInfos = userInfoService.ListUserInfo();
            request.setAttribute("userInfos", userInfos);
            request.getRequestDispatcher("/pages/tab/tab-user.jsp").forward(request, response);
        } else if (type.equals("dept")) {
            //分页查询部门数据
            Integer page = Integer.valueOf(request.getParameter("currentpage"));
            String ptype = request.getParameter("ptype");
            Map<String, Integer> pageMap = deptPageService.getPage(page, ptype);
            Integer currentPage = pageMap.get("page");
            Integer totalPage = pageMap.get("maxPage");
            Integer totalRecord = pageMap.get("maxPage");
            //查询部门列表数据
//            List<Dept> depts = deptService.findAllDepts();
            //分页查询部门数据
            List<Dept> depts = deptService.pageDepts(currentPage, PageUtil.getPageSize());
            request.getSession().removeAttribute("currentPage");
            request.getSession().removeAttribute("totalRecord");
            request.getSession().removeAttribute("totalPage");
            request.getSession().removeAttribute("totalPage");
            request.getSession().removeAttribute("depts");
            request.setAttribute("currentPage", currentPage);
            request.setAttribute("totalRecord", totalRecord);
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("depts", depts);
            request.getRequestDispatcher("/pages/tab/tab-dept.jsp").forward(request, response);
        }
    }

}
